'use strict'

module.exports = options => async (ctx, next) => {
  const { token = ctx.query.SESSION_ID } = ctx.headers
  ctx.user = {}
  if (token) {
    try {
      const key = token.split('__')[0]
      const user = JSON.parse(await ctx.app.redis.get(key))
      if (user.password === token) {
        user.key = key
        ctx.user = user
      }
    } catch (e) {
      console.log('解析失败', e)
    }

    if (ctx.user._id) {
      console.log('loader', ctx.user.username)
    }
  }

  await next()
}
